$(function () {
    $(document).on("pageInit", "#page-list", function (e, id, page) {
        $(document).on("click", '.deleteProduct', function () {
            // 添加到黑名单
            if (!confirm('确认此操作?')) {
                return false;
            }
            var product_id = $(this).attr('title');
            $.get('/tool/addBlack', {product_id: product_id}, function (ret) {
                $.alert(ret);
            });
            return false;
        });

        $(document).on("click", '.deleteConnect', function () {
            // 取消收藏
            var product_id = $(this).attr('title');
            $.get('/connect/delete', {product_id: product_id}, function (ret) {
                $('#card-' + product_id).html('');
            });
            return false;
        });

        $('.buttons-tab').find('a').click(function () {
            var attr = $(this).attr('href');
            if (attr == '#hot') {
                window.location.href = '/index/hot';
            }
            if (attr == '#tui') {
                window.location.href = '/index/tui';
            }
            if (attr == '#mall') {
                window.location.href = '/index/mall';
            }
        });
    });

    var winw = $(window).width();
    var width = winw * 0.97;
    $(document).on("pageInit", "#page-item", function (e, id, page) {
        $(".small_images, .main-image").find("img").each(function () {
            var alt = $(this).attr("alt");
            if (width) {
                $(this).attr('src', alt).width(width);
            } else {
                $(this).attr('src', alt);
            }
        });

        $('.connect').click(function () {
            // 收藏
            var product_id = $(this).attr('title');
            $.get('/connect/get', {product_id: product_id}, function (ret) {
                $.alert(ret)
            });
            return false;
        });

        $('.copyTaocode').click(function () {
            var isOk = false;
            if (document.queryCommandSupported("Copy")) {
                var val = document.getElementById('item-note');
                window.getSelection().selectAllChildren(val);
                $('#item-note').focus().select();
                try {
                    if (document.execCommand('copy', false, null)) {
                        isOk = true;
                    }
                } catch (err) {
                    console.log(err);
                }
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            }
            if (isOk) {
                var product_id = $('#product_id').val();
                $.get('/index/useritem', {product_id: product_id});
                $.alert("口令码已准备好, 打开【手机淘宝】查看购买");
            } else {
                $.alert("口令码复制失败, 请手动复制或联系管理员, 管理员微信adqin1984");
            }
        });

        $('.goShare').click(function () {
            var isOk = false;
            if (document.queryCommandSupported("Copy")) {
                var val = document.getElementById('item-note');
                window.getSelection().selectAllChildren(val);
                $('#item-note').focus().select();
                try {
                    if (document.execCommand('copy', false, null)) {
                        isOk = true;
                    }
                } catch (err) {
                    console.log(err);
                }
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            }
            if (isOk) {
                var product_id = $('#product_id').val();
                $.get('/index/useritem', {product_id: product_id});
                $.alert("商品分享文案已复制成功，快去粘贴分享给好友吧！");
            } else {
                $.alert("商品分享文案复制失败, 请手动复制或联系管理员, 管理员微信adqin1984");
            }
        });
    });

    $(document).on("pageInit", "#page-share", function (e, id, page) {
        var alt = $('.main-image > img').attr('alt');
        $('.main-image > img').attr('src', alt).width(width);
    });

    $.init();
});